--
-- Author: yawei.zhu
-- Date: 2018-12-14 17:12:23
--
-- PvpChampionPanel
--
local PvpChampionBottom = import(".PvpChampionBottom")
local PvpChampionRender = import(".renders.PvpChampionRender")

local ClassRef = app.mvc.createPanelClass(...)

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.PvpChampionNode))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)

    self._championList = self._ui.ListView
    self._championList.createRender = function (list, idx)
        local items = list:getItems()
        local render = items[idx]
        if nil == render then
            render = PvpChampionRender.create(nil, idx)
            render:onOperate(self._onOperate)
            list:pushBackCustomItem(render)
        end
        render:setVisible(true)
        return render
    end

    self._pvpChampionBottom = PvpChampionBottom.new(self._ui.Panel_bom, self)
    self._pvpChampionBottom:onOperate(function(op, data)
        executeFunc(self._onOperate, op, data)
    end)
end

function ClassRef:clearList()
    -- 清空list
    self._championList:removeAllChildren()
end

function ClassRef:refresh()
    if not self._data then return end
end

function ClassRef:updateView(seasonInfo)
    self._ui.title:setString(seasonInfo:getTitle())
    self._ui.data:setString(seasonInfo:getDateStr())
        
    self._pvpChampionBottom:refresh(seasonInfo)

    -- 更新list数据
    local list = self._championList
    local items = list:getItems()
    if #items == 1 + 7 then -- 一个占位item + 一周7天
        for i = 1, 7 do
            local item = items[i + 1]
            local throneInfo = seasonInfo:getThroneInfo(i)
            item:setThroneGame( seasonInfo:getThroneGame() )
            item:setData(throneInfo)
        end
    else
        self:setChampionList(list, seasonInfo)
    end
end

-- 设置冠军赛list
function ClassRef:setChampionList(list, seasonInfo)
    list:removeAllChildren()
    -- 添加占位item
    local item = ccui.Widget:create()
    item:setContentSize(5, 5)
    list:pushBackCustomItem(item)
    -- 获取当前周几
    local day = app:getWeekDay()

    for i = 1, 7 do
        local render = list:createRender(i + 1)
        render:onBtnClick(function(sender)
            local data = sender:getData()
            if data == nil then return end

            if data.day == day then -- 当前天
                local status = sender:getStatus()
                -- local championInfo = self._model:getChampionInfo()

                if seasonInfo:getRegister() then
                    app:sendMsg("PvpChampionController", "openView", data)
                else
                    if status == Enums.ChampionStatus.NOT_OPEN then
                        display.pushToast( L("champion_register1") )
                    else
                        app:sendMsg("PvpChampionController", "openView", data)
                    end
                end
            else
                -- 非当前天
                local baseInfo = data.baseInfo
                if not baseInfo then -- 没有产生冠军， 表示没有对战信息
                    display.pushToast(L("champion_notice3"))
                else
                    app:sendMsg("PvpChampionController", "openOtherDayFormView", data)
                end
            end

        end)

        local throneInfo = seasonInfo:getThroneInfo(i)
        render:setThroneGame( seasonInfo:getThroneGame() )
        render:setData(throneInfo)
    end

    list:refreshView()
    -- 显示优化，设置当前居中
    if day >= 5 then
        list:jumpToRight()
    end
end

function ClassRef:onOperate(cb)
    self._onOperate = cb
end

return ClassRef
